/*
 * ChatPanel.java
 *
 * Created on September 21, 2008, 4:33 PM
 */
package no.eirikb.bomberman.applet.chat;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;
import javax.swing.JButton;
import javax.swing.JPanel;
import no.eirikb.bomberman.applet.Bomberman;
import no.eirikb.bomberman.shared.message.Message;
import no.eirikb.bomberman.shared.message.GameMessage;
import no.eirikb.bomberman.shared.message.PrivateMessage;
import no.eirikb.bomberman.shared.message.PublicMessage;

/**
 *
 * @author  eirikb
 */
public class ChatContainerPanel extends javax.swing.JPanel {

    private Bomberman bomberman;
    private ChatPanel publicChatPanel;
    private ChatPanel gameChatPanel;
    private Hashtable<String, ChatPanel> privateChatPanels;

    /** Creates new form ChatPanel */
    public ChatContainerPanel(Bomberman bomberman) {
        this.bomberman = bomberman;
        privateChatPanels = new Hashtable<String, ChatPanel>();
        initComponents();
    }

    public void chat(Message message) {
        if (message instanceof PublicMessage) {
            publicChatPanel.chat(message);
        } else if (message instanceof GameMessage) {
            gameChatPanel.chat(message);
        } else if (message instanceof PrivateMessage) {
            PrivateMessage privMessage = (PrivateMessage) message;
            ChatPanel priv;
            if ((priv = privateChatPanels.get(privMessage.getFrom())) != null) {
                priv.chat(privMessage);
            } else {
                priv = new ChatPanel(bomberman, privMessage.getFrom());
                privateChatPanels.put(privMessage.getFrom(), priv);
                JPanel privPanel = new JPanel(new BorderLayout());
                JButton closeButton = new JButton("Close window");
                final String nick = privMessage.getFrom();
                closeButton.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent arg0) {
                        close(nick);

                    }
                });
                privPanel.add(BorderLayout.NORTH, closeButton);
                chatPane.add(privMessage.getFrom(), priv);
                priv.chat(message);
            }
        }
    }

    private void close(String nick) {
        chatPane.remove(privateChatPanels.get(nick));
        privateChatPanels.remove(nick);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        chatPane = new javax.swing.JTabbedPane();

        setLayout(new java.awt.BorderLayout());

        chatPane.setMaximumSize(new java.awt.Dimension(640, 100));
        chatPane.setPreferredSize(new java.awt.Dimension(60, 150));
        chatPane.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                chatPanePropertyChange(evt);
            }
        });
        add(chatPane, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void chatPanePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_chatPanePropertyChange
    publicChatPanel = new ChatPanel(bomberman, null);
    gameChatPanel = new ChatPanel(bomberman, null);
    chatPane.add("Public", publicChatPanel);
    chatPane.add("Game", gameChatPanel);
}//GEN-LAST:event_chatPanePropertyChange
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane chatPane;
    // End of variables declaration//GEN-END:variables
}
